package com.xdb.sqlparser.ast;

import com.xdb.sqlparser.ast.statement.SQLSelectOrderByItem;
import com.xdb.sqlparser.visitor.SQLAstVisitor;

import java.util.ArrayList;
import java.util.List;

public class SQLOrderBy extends AbstractSQLObjectImpl {

	protected final List<SQLSelectOrderByItem> items = new ArrayList<SQLSelectOrderByItem>();

	public SQLOrderBy() {

	}

	public SQLOrderBy(SQLExpr expr) {
		SQLSelectOrderByItem item = new SQLSelectOrderByItem(expr);
		addItem(item);
	}

	public void addItem(SQLSelectOrderByItem item) {
		if (item != null) {
			item.setParent(this);
		}
		this.items.add(item);
	}

	public List<SQLSelectOrderByItem> getItems() {
		return this.items;
	}

	protected void accept0(SQLAstVisitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.items);
		}

		visitor.endVisit(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + items.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SQLOrderBy other = (SQLOrderBy) obj;
		return items.equals(other.items);
	}

	public void addItem(SQLExpr expr, SQLOrderingSpecification type) {
		SQLSelectOrderByItem item = createItem();
		item.setExpr(expr);
		item.setType(type);
		addItem(item);
	}

	protected SQLSelectOrderByItem createItem() {
		return new SQLSelectOrderByItem();
	}
}
